package controller

import (
	"common/helper"
	myUserHelp "common/userHelp"
	"github.com/valyala/fasthttp"
	"member/model"
)

type PromoTreasureController struct{}

// 宝箱活动-配置
func (that PromoTreasureController) Config(ctx *fasthttp.RequestCtx) {

	s, err := model.PromoTreasureConfigList()
	if err != nil {
		helper.Print(ctx, false, err.Error())
		return
	}

	helper.Print(ctx, true, s)
}

// 宝箱活动-申请记录
func (that PromoTreasureController) Record(ctx *fasthttp.RequestCtx) {

	uid := model.GetUidFromToken(ctx)
	if uid == "" {
		helper.Print(ctx, false, helper.AccessTokenExpires)
		return
	}

	// 代理
	if myUserHelp.GetMemberTester(uid) == 2 {
		helper.Print(ctx, false, helper.PromoApplyFailed)
		return
	}

	s, err := model.PromoTreasureMemberRecord(uid)
	if err != nil {
		helper.Print(ctx, false, err.Error())
		return
	}

	helper.Print(ctx, true, s)
}

// 宝箱活动-申请
func (that PromoTreasureController) Apply(ctx *fasthttp.RequestCtx) {

	inviteNum := ctx.QueryArgs().GetUintOrZero("invite_num")
	if inviteNum <= 0 {
		helper.Print(ctx, false, helper.ParamErr)
		return
	}

	uid := model.GetUidFromToken(ctx)
	if uid == "" {
		helper.Print(ctx, false, helper.AccessTokenExpires)
		return
	}

	// 代理
	if myUserHelp.GetMemberTester(uid) == 2 {
		helper.Print(ctx, false, helper.PromoApplyFailed)
		return
	}

	err := model.PromoTreasureApply(uid, inviteNum)
	if err != nil {
		helper.Print(ctx, false, err.Error())
		return
	}

	helper.Print(ctx, true, helper.Success)
}
